﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class flowSubType : System.Web.UI.Page
{
    SubFlow sf = new SubFlow();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] == null || Session["roleID"] == null)
        {
            Response.Redirect(GetUrlPath.GetUrl + "/Login.aspx");
            Response.End();
        }
        if (!IsPostBack)
        {
            BindFatherFlow();
            BindFlow();
        }

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {


        PanelAdd.Visible = true;
        PanelChange.Visible = false;
        PanelDelete.Visible = false;
    }
    protected void btnChange_Click(object sender, EventArgs e)
    {
        BindFlow();
        PanelChange.Visible = true;
        PanelAdd.Visible = false;
        PanelDelete.Visible = false;

    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        BindFlow();
        PanelDelete.Visible = true;
        PanelChange.Visible = false;
        PanelAdd.Visible = false;

    }
    /// <summary>
    /// 服务端认证，流程名称是否已经存在
    /// </summary>
    /// <param name="source"></param>
    /// <param name="args"></param>
    protected void CustomValidAddFlowName_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string flowName = args.Value;
        if (sf.FindFlow(flowName))
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }

    }
    protected void CustomValidChangeFlow_ServerValidate(object source, ServerValidateEventArgs args)
    {
        CustomValidAddFlowName_ServerValidate(source, args);
    }
    /// <summary>
    /// 添加流程
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddFlow_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            SubFlow newFlow = new SubFlow(Convert.ToInt32(ddlAddFatherFlow.SelectedValue), txtAddFlowName.Text.Trim(), txtAddBrief.Text.Trim());
            if (newFlow.AddFlow())
            {
                Response.Write("<script language='javascript'>alert('添加成功！')</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('在尝试将新流程写入数据库时，发生了意外，添加失败，请联系管理员！')</script>");
            }
        }

    }
    /// <summary>
    /// 绑定父流程
    /// </summary>
    protected void BindFatherFlow()
    {
        DataSet ds = sf.FindFatherFlow();
        ddlAddFatherFlow.DataSource = ds;
        ddlAddFatherFlow.DataTextField = "TYPENAME";
        ddlAddFatherFlow.DataValueField = "FLOWTYPEID";
        ddlChangeFatherFlow.DataSource = ds;
        ddlChangeFatherFlow.DataTextField = "TYPENAME";
        ddlChangeFatherFlow.DataValueField = "FLOWTYPEID";
        ddlAddFatherFlow.DataBind();
        ddlChangeFatherFlow.DataBind();
    }
    /// <summary>
    /// 读取系统中存在的二级流程
    /// </summary>
    protected void BindFlow()
    {
        DataSet ds = sf.GetFlow();
        ddlChangeFlow.DataSource = ds;
        ddlChangeFlow.DataTextField = "SUBTYPENAME";
        ddlChangeFlow.DataValueField = "FLOWSUBTYPEID";
        ddlDeleteFlow.DataSource = ds;
        ddlDeleteFlow.DataTextField = "SUBTYPENAME";
        ddlDeleteFlow.DataValueField = "FLOWSUBTYPEID";
        ddlChangeFlow.DataBind();
        ddlDeleteFlow.DataBind();
    }
    /// <summary>
    /// 修改流程
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSure_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            SubFlow newFlow = new SubFlow(Convert.ToInt32(ddlChangeFatherFlow.SelectedValue), txtChangeFlowName.Text.Trim(), txtChangeBrief.Text.Trim());
            if (newFlow.ChangeFlow(Convert.ToInt32(ddlChangeFlow.SelectedValue)))
            {
                Response.Write("<script language='javascript'>alert('修改成功！')</script>");
                BindFlow();
                txtChangeFlowName.Text = "";
                txtChangeBrief.Text = "";
            }
            else
            {
                Response.Write("<script language='javascript'>alert('在尝试修改流程信息时，发生了意外，修改失败，请联系管理员！')</script>");
            }
        }

    }

    /// <summary>
    /// 删除流程
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDeleteFlow_Click(object sender, EventArgs e)
    {
        int flowID = Convert.ToInt32(ddlDeleteFlow.SelectedValue);
        if (sf.DeleteFlow(flowID))
        {
            Response.Write("<script language='javascript'>alert('删除成功！')</script>");
            BindFlow();
        }
        else
        {
            Response.Write("<script language='javascript'>alert('在尝试删除流程时，发生了意外，删除失败，请联系管理员！')</script>");
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

        PanelDelete.Visible = false;
        PanelChange.Visible = false;
        PanelAdd.Visible = true;
    }
  
}
